var bindPhone = {
	reg :  /^0?(13|14|15|17|18)[0-9]{9}$/,
	code:"",
	phone:""
};

//保存手机号
$("#btn-save").on("click",function(){
	var phone = $("#phone").val();
	var code = $("#code").val();
	var options = {
		phone:phone,
		authCode:code,
		userId:Feng.userId
	};

	if(!bindPhone.validetor(phone)){
		return
	}
	
	if(!code){
		Feng.error("请输入验证码");
		return;
	}

	//保存手机号
	var ajax = new $ax(Feng.ctxPath+"/user/boundPhone", function(data){        
        if (data.success){
        	//时间
        	if(bindPhone.phone!=="" && bindPhone.phone !== null){
				Feng.success("手机号修改成功");
				localStorage.removeItem("phone");
				setTimeout(function(){
					window.location.href = "ui-me.html";
				},2000);   
			}
			else{
				Feng.success("手机号绑定成功");
				handSelCoupon.init(data);
				
			}
			    	
        	
        }
        else if(data.code === 2){
        	Feng.error(data.msg);
        }
        else{
        	Feng.error("手机号绑定失败");
        }        
   },function(data){    	
        Feng.error("手机号绑定失败");
    });
    ajax.set(options);
    ajax.start();
});

$getCode = $("#btn-send-code");
   /* 定义参数 */
$getCode.sendCode({
        disClass: 'btn-disabled',
        secs: 60,
        run: false,
        runStr: '{%s}秒后重新获取',
        resetStr: '重新获取验证码'
    });
 $getCode.on('click', function () {
        /* ajax 成功发送验证码后调用【start】 */
//      YDUI.dialog.loading.open('发送中');
        
    var phone = $("#phone").val();	
	var options = {
		phone:phone	
	};
	
	if(!bindPhone.validetor(phone)){
		return
	}
	//发送验证码	
	var ajax = new $ax(Feng.ctxPath+"/user/sendAuthCode", function(data){        
        if (data.success){
        	Feng.success("验证码发送成功");
        	//调用
        	 $getCode.sendCode('start');
        }
        else if(data.code === 2){
        	Feng.error(data.msg);
        }
        else{
        	Feng.error("验证码发送失败");
        }        
   },function(data){    	
        Feng.error("验证码发送失败");
    });
    ajax.set(options);
    ajax.start();
        
//      setTimeout(function(){
            
//          YDUI.dialog.loading.close();
            
            $getCode.sendCode('start');
//          YDUI.dialog.toast('已发送', 'success', 1500);
//          
//      }, 1500);
    });
//发送验证码
/*document.getElementById("btn-send-code").addEventListener("click",function(){
	
});*/



//手机号验证
bindPhone.validetor = function(phone){
	if(!phone){
		Feng.error("请输入手机号");
		return false;
	}	
	
	if(!bindPhone.reg.test(phone)){
		Feng.error("请输入正确手机号");
		return false;
	}
	
	return true;
}

//赠送优惠券


$(function(){
	bindPhone.phone = localStorage.getItem("phone");
	if(bindPhone.phone!=="" && bindPhone.phone !== null){
		$("#phone").val(bindPhone.phone);
//		$("#phone")[0].disabled = true;
		$(".mui-title").text("修改手机号");
	}
	else{
		$(".mui-title").text("绑定手机号");
	}
});
